#include "srmediaplayer.h"
#include "ui_srmediaplayer.h"
#include "mediaplaylist.h"

#include <QFileDialog>
#include <QSlider>
#include <QAudioOutput>
#include <QMessageBox>

SRMediaPlayer::SRMediaPlayer(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::SRMediaPlayer)
{
    ui->setupUi(this);
    setWindowTitle("SR音视频播放器");   // 设置窗口标题

    // 建立对象
    qPlayer = new QMediaPlayer;                        // 播放器对象
    qPlaylist = new MediaPlaylist(this);               // 创建播放列表的对象
    qVideoWidget = new QVideoWidget(ui->VideoWidget);  // 播放窗口对象

    // 设置播放器窗口
    qPlayer->setVideoOutput(qVideoWidget);
    QAudioOutput *audioOutput = new QAudioOutput; // 创建音频输出对象
    qPlayer->setAudioOutput(audioOutput);         // 将音频输出对象绑定到播放器

    // 信号与槽
    connect(ui->ExitButton, &QPushButton::clicked, this, &QWidget::close);  // 设置关闭按钮
}

void SRMediaPlayer::paintEvent(QPaintEvent *e)
{
    qVideoWidget->resize(ui->VideoWidget->size());
}


SRMediaPlayer::~SRMediaPlayer()
{
    delete ui;
    delete qPlayer;
    delete qVideoWidget;
    delete qPlaylist;
}

void SRMediaPlayer::on_BrowseButton_clicked()
{
    // 注意路径要用反斜杠表示
    QStringList filenames = QFileDialog::getOpenFileNames(this,"你的电脑可否有好看的视频？",
                                                          "C:/Users/Administrator/Desktop",
                                                          "allfiles(*.*);;" "MP4(*.mp4);;" "MP3(*.mp4);;""MKV(*.mkv);;""AVI(*.AVI)");

    // 将文件添加到播放列表
    if (!filenames.isEmpty()) {
        foreach (QString filename, filenames) {
            QUrl url = QUrl::fromLocalFile(filename);
            qPlaylist->addMedia(url);
        }
    }
}

// 播放按钮
void SRMediaPlayer::on_PlayButton_clicked()
{
    if (!qPlaylist->isEmpty()) {
        // 获取播放器状态，如果暂停就继续播放
        if (qPlayer->playbackState() == QMediaPlayer::PausedState) {
            qPlayer->play();
        }
        // 如果播放器不在暂停状态，设置源并播放
        else {
            QUrl nextMedia = qPlaylist->currentMedia();
            updateFilenameDisplay(nextMedia);
            qPlayer->setSource(nextMedia);
            qPlayer->play();
        }
    }
}

// 暂停按钮
void SRMediaPlayer::on_PauseButton_clicked()
{
    qPlayer->pause();    
}

// 下一个按钮
void SRMediaPlayer::on_NextButton_clicked()
{
    if (!qPlaylist->isEmpty()) {
        qPlaylist->next();
        QUrl currentMedia = qPlaylist->currentMedia();
        updateFilenameDisplay(currentMedia);
        qPlayer->setSource(currentMedia);
        qPlayer->play();
    } else {
        QMessageBox::warning(this, "警告", "播放列表为空！");
    }
}

// 上一个按钮
void SRMediaPlayer::on_PreviousButton_clicked()
{
    if (!qPlaylist->isEmpty()) {
        qPlaylist->previous();
        QUrl currentMedia = qPlaylist->currentMedia();
        updateFilenameDisplay(currentMedia);
        qPlayer->setSource(currentMedia);
        qPlayer->play();
    } else {
        QMessageBox::warning(this, "警告", "播放列表为空！");
    }
}

void SRMediaPlayer::updateFilenameDisplay(const QUrl &media)
{
    if (!media.isEmpty()) {
        QString filename = media.toLocalFile(); // 获取本地文件路径
        ui->FilenameEdit->setText(filename);    // 更新 QLineEdit 的内容
    } else {
        ui->FilenameEdit->clear();              // 如果没有媒体文件，清空 QLineEdit
    }
}
